/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.util;

import org.eclipse.jface.databinding.swt.WidgetValueProperty;
import org.eclipse.nebula.widgets.gallery.Gallery;
import org.eclipse.nebula.widgets.gallery.GalleryItem;
import org.eclipse.swt.SWT;

/**
 * 
 * 
 * @since 1.0.0
 */
public class GalleryWidgetValueProperty extends WidgetValueProperty {

	/** ... TODO Javadoc ... */
	private Gallery gallery;

	/**
	 * 
	 */
	public GalleryWidgetValueProperty() {
		super(SWT.Selection);
	}

	/**
	 * 
	 */
	public Object getValueType() {
		return String.class;
	}

	/**
	 * 
	 */
	protected Object doGetValue(Object source) {
		gallery = (Gallery) source;
		if (gallery != null) {
			if (gallery.getSelection().length > 0) {
				GalleryItem item = gallery.getSelection()[0];
				return item.getData("filePath");
			}
		}
		return null;
	}

	/**
	 * 
	 */
	protected void doSetValue(Object source, Object value) {
		Gallery gallery = (Gallery) source;
		if (value != null) {
			GalleryItem applications = gallery.getItem(0);
			for (GalleryItem item : applications.getItems()) {
				String filepath = (String) item.getData("filePath"); 
				if (filepath != null && filepath.equals((String) value)) {
					gallery.setSelection(new GalleryItem[] { item });
					break;
				}
			}
		}
	}

}
